package handler

import (
	"github.com/labstack/echo/v4"
	"log"
	"strconv"
	"time"
	"ulcdkey/db"
	"ulcdkey/tool"
)

// 查询兑换日志
func GetExchangeLog(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	nLog := new(db.ExchangeLog)

	startTime := c.FormValue("startTime")
	endTime := c.FormValue("endTime")
	bCdk := c.FormValue("cdk")

	gameId, err := strconv.Atoi(c.FormValue("gameId"))
	if err != nil {
		log.Println("GetExchangeLog err:", err)
		r.Code = -1
		r.Msg = "gameId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	cdkTypeId, err := strconv.Atoi(c.FormValue("cdkTypeId"))
	if err != nil {
		log.Println("GetExchangeLog err:", err)
		r.Code = -1
		r.Msg = "cdkTypeId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	as, inValid, err := nLog.GetExchangeLog(bCdk, startTime, endTime, gameId, cdkTypeId)
	if err != nil {
		log.Println("GetExchangeLog err:", err)
		r.Code = -1
		r.Msg = "查询日志失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	r.Data = map[string]interface{}{
		"result":  as,
		"inValid": inValid,
	}

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}
